package org.flashamaphone.voices.waveformFactories
{
	import flash.utils.ByteArray;
	import org.flashamaphone.buffer.SoundConstants;
	
	/** Generates byte array cycles of a particular type of waveform */
	public class WaveFactory implements IWaveFactory
	{
		public static const MULTIPLIER:Number = (2*Math.PI)/SoundConstants.SAMPLING_RATE;
		
		/** last sample index */
		protected var _lastSampleIndex:int = 0;

		/** 
		 * getter for last sample index - helpful for ensuring seamless stitchin of sounds
		 * 
		 * @return index
		 */
		public function get lastSampleIndex():int {
			return _lastSampleIndex;
		}
		
		/** 
		 * get a sample at a specific phase of a cycle
		 * 
		 * @return index
		 */
		public function sample(phase:Number):Number {
			return 0;
		}
		
		/** 
		 * get the byte array from a complete cycle
		 * 
		 * @param frequency
		 * @param number of samples
		 * @param offset
		 * @return byte array of cycle
		 */
		public function cycle(freq:Number, length:int, offset:int = 0):ByteArray {
			var returnBytes:ByteArray = new ByteArray();
			for (var i:int=offset; i < length + offset; i++ ) {
				var position:Number = i * WaveFactory.MULTIPLIER * freq;
				var sample:Number = sample(position);
				returnBytes.writeFloat(sample);
				returnBytes.writeFloat(sample);
			}
			_lastSampleIndex = i;
			return returnBytes;
		}
	}
}